package com.sw.watcher;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.els.ElsConnection;
import com.els.factory.ElasticFactory;
import com.google.gson.JsonObject;
import com.sw.entity.Log;

public class ODRWatcher extends BaseWatcher implements Watcher,Job{
	public ODRWatcher(){
		url="http://yundr.gov.cn/";
		module="ODR本站";
		sub_module="首页访问";
	}

	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		run();
	}
	@Override
	public void run(){
		ElsConnection ec=ElasticFactory.getConnection();
		DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String now=df.format(new Date());
		boolean success=watch();
		Log log=new Log();
		log.setModule(module);
		log.setTime(now);
		log.setSub_module(sub_module);
		if(success){
			log.setRun(true);
			logger.info("["+module+"]["+sub_module+"]"+"运行正常。");
			List<JsonObject> contacts = ec.queryAll("system", "notice");
			for (JsonObject obj : contacts) {
				if(obj.get("active").getAsBoolean()&&"admin".equals(obj.get("role").getAsString())){
					sms.warning(obj.get("tel").getAsString(), module, "成功");
					logger.info("发送短信给用户："+obj.toString());
				}
			}
		}else{
			//发送通知
			log.setRun(false);
			log.setRemark("失败!");
			logger.error("["+module+"]["+sub_module+"]"+"运行失败！");
			List<JsonObject> contacts = ec.queryAll("system", "notice");
			for (JsonObject obj : contacts) {
				if(obj.get("active").getAsBoolean()&&"admin".equals(obj.get("role").getAsString())){
					sms.warning(obj.get("tel").getAsString(), module, sub_module);
					logger.info("发送短信给用户："+obj.toString());
				}
			}
		}
		ec.insertOne("log", "log", null, log.json());
	}
}
